package com.ztgf.file.controller;

import com.ztgf.file.fastdfs.FastdfsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
@Slf4j
@Api(value = "TestController",tags = "测试")
public class TestController {

    @Autowired
    private FastdfsUtils fastdfsUtils;



    //使用fastdfs进行文件上传
    @ApiOperation(value = "上传文件",notes = "上传文件")
    @PostMapping("/uploadFilePath")
    public String uploadFilePath(@RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        if(uploadFile.isEmpty()){
            return  "上传文件不能为空";
        }
        try {
            String path = fastdfsUtils.uploadFilePath(uploadFile);
            return "path:"+path;
        }catch (Exception  e){
            e.printStackTrace();
            return "上传失败，请重新尝试";
        }
    }

    //使用fastdfs进行文件上传
    @ApiOperation(value = "上传文件",notes = "上传文件")
    @PostMapping("/uploadFile")
    public String uoloadFileToFast(@RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        if(uploadFile.isEmpty()){
            return  "上传文件不能为空";
        }
        try {
            String path = fastdfsUtils.uploadFile(uploadFile);
            return "path:"+path;
        }catch (Exception  e){
            e.printStackTrace();
            return "上传失败，请重新尝试";
        }
    }
}
